package com.hesicare.cgm.controller;

import com.hesicare.cgm.entity.CgmPatientGlucose;
import com.hesicare.cgm.entity.CgmResult;
import com.hesicare.cgm.enums.PatientTypeEnum;
import com.hesicare.cgm.service.ICgmPatientBloodService;
import com.hesicare.config.cache.Cache;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.service.ISysPatientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 55097
 * @create 2021/12/5 14:44
 */
@RestController
@RequestMapping("/getPatientBlood")
public class CgmController {

    @Autowired
    private Cache cache;

    @Autowired
    private ICgmPatientBloodService icgMpatientBloodService;

    @Autowired
    private ISysPatientService ISysPatientService;

    protected static final Logger log = LoggerFactory.getLogger(CgmController.class);

    @PostMapping("message")
    public CgmResult getPatientGlucoseMessage(@RequestBody CgmPatientGlucose patientGlucose) {
        log.info("接收CGM推送数据{}", patientGlucose.toString());
        SysPatient sysPatient = ISysPatientService.selectSysPatientByHospitalizedNum(patientGlucose.getNumber());
        if (sysPatient == null) {
            return CgmResult.fail("查无该患者");
        }
        if (PatientTypeEnum.ISBINDING.getType() != sysPatient.getBindingStatus()){
            sysPatient.setBindingStatus(PatientTypeEnum.ISBINDING.getType());
            ISysPatientService.updateSysPatient(sysPatient);
        }
        if (!icgMpatientBloodService.getCGMpatientBlood(patientGlucose, sysPatient)) {
            return CgmResult.fail("fail");
        }
        return CgmResult.success("success");
    }
}
